package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * 该线程任务负责与指定的客户端完成HTTP交互
 * 每次HTTP交互都采取一问一答的规则，因此交互由三步来完成:
 * 1:解析请求
 * 2:处理请求
 * 3:发送响应
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public String line;
    @Override
    public void run() {
        try {
            HttpServletRequest request=new HttpServletRequest(socket);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private  String readLine() throws IOException {
        InputStream in = socket.getInputStream();
        StringBuilder builder = new StringBuilder();
        int d;
        char pre='a',cur='a';//pre上一次读取的字符  cur本次读取到的字符
        while((d = in.read())!=-1){
            cur = (char)d;//将本地读取的字节转换为字符赋值给cur
            if(pre==13&cur==10){//是否连续读取到了回车+换行
                break;
            }
            builder.append(cur);//将本次读取的字符拼接到StringBuilder中
            pre = cur;//在进行下一个字符读取前将本地读取的字符记录为上次读取的字符
        }
        return builder.toString().trim();
    }
}






